package function;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import UserDatabase.UserDatabase;

public class MUserFrame extends JFrame{
	String [] tableHead;
	String [][] user;
	JTable table;
	JLabel l_id,l_name,l_type,l_age,l_uno,l_hno;
	JTextField t_id,t_name,t_age,t_uno,t_hno;
	JComboBox<String> c_type ;
	JButton b_find , b_reset,b_updata,b_refresh;
	UserDatabase udb = new UserDatabase();
	String SQL;
	
	public MUserFrame(String SQL) {
		this.setSize(800,500);
		this.setTitle("业主管理");
		this.setLocation(500,150);
		this.setLayout(new BorderLayout());
		this.SQL = SQL;
		init();
		this.setVisible(true);
	}
	
	public void init() {
		udb.setSQL(SQL);
		user = udb.getRecord();
		tableHead = udb.getColumnName();
		table = new JTable(user,tableHead);
		
		l_id = new JLabel("ID",JLabel.CENTER);
		l_name = new JLabel("姓名",JLabel.CENTER);
		l_type = new JLabel("身份",JLabel.CENTER);
		l_age = new JLabel("年龄",JLabel.CENTER);
		l_uno = new JLabel("单元号",JLabel.CENTER);
		l_hno = new JLabel("房间号",JLabel.CENTER);
		
		
		t_id = new JTextField();
		t_name = new JTextField();
		c_type = new JComboBox<String>();
		c_type.addItem("");
		c_type.addItem("业主");
		c_type.addItem("物业");
		t_age = new JTextField();
		t_uno = new JTextField();
		t_hno = new JTextField();
		b_find = new JButton("查询");
		b_find.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String SQL = "select * from user where";
				String id = t_id.getText();
				String name = t_name.getText();
				String type = (String) c_type.getSelectedItem();
				String age = t_age.getText();
				String uno = t_uno.getText();
				String hno = t_hno.getText();
				if(id.equals("")&&name.equals("")&&type.equals("")&&age.equals("")&&uno.equals("")&&hno.equals("")) {
					new MUserFrame("select * from user");
					setVisible(false);
				}else if(!id.equals("")) {
					
					SQL = SQL+" id = '"+id+"' ";
					new MUserFrame(SQL);
					setVisible(false);
				
				}else if(!name.equals("")) {
				
					SQL = SQL+" name = '"+name+"' ";
					new MUserFrame(SQL);
					setVisible(false);
					
				}else if(!type.equals("")) {
				
					SQL = SQL+" type = '"+type+"' ";
					new MUserFrame(SQL);
					setVisible(false);
				
				}else if(!age.equals("")) {
				
					SQL = SQL+" age = '"+age+"' ";
					new MUserFrame(SQL);
					setVisible(false);
				
				}else if(!uno.equals("")) {
				
					SQL = SQL+" uno = '"+uno+"' ";
					new MUserFrame(SQL);
					setVisible(false);
				
				}else if(!hno.equals("")) {
					
					SQL = SQL+" hno = '"+hno+"' ";
					new MUserFrame(SQL);
					setVisible(false);
					
				}
				
			}
			
		});
		b_reset = new JButton("重置");
		b_reset.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				t_id.setText(null);
				t_name.setText(null);
				c_type.setSelectedItem("");
				t_age.setText(null);
				t_uno.setText(null);
				t_hno.setText(null);
			}
			
		});
		b_updata = new JButton("更新用户数据");
		b_updata.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				new UpdateUserFrame();
			}
			
		});
		b_refresh = new JButton("刷新");
		b_refresh.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				new MUserFrame("select * from user");
				setVisible(false);
			}
			
		});
		
		JPanel p = new JPanel();
		p.setLayout(new GridLayout(2,7,5,5));
		p.add(l_id);
		p.add(l_name);
		p.add(l_type);
		p.add(l_age);
		p.add(l_uno);
		p.add(l_hno);
		p.add(b_reset);
		p.add(t_id);
		p.add(t_name);
		p.add(c_type);
		p.add(t_age);
		p.add(t_uno);
		p.add(t_hno);
		p.add(b_find);
		
		JScrollPane sp = new JScrollPane(table);
		
		JPanel p_bottom = new JPanel();
		p_bottom.setLayout(new GridLayout(1,2,8,8));
		p_bottom.add(b_updata);
		p_bottom.add(b_refresh);
		
		this.add(p,BorderLayout.NORTH);
		this.add(sp,BorderLayout.CENTER);
		this.add(p_bottom,BorderLayout.SOUTH);
		
	}
	
}
